package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsSyncIotProduct;
import com.econ.powercloud.ems.dal.mapper.EmsSyncIotProductMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author : zjf
 * @Description : iotProductSync(iot2.0产品同步数据) DAO层
 * @Date : 2021/11/16 11:43
 * @Modified by : zjf
 */
@Repository
public class EmsSyncIotProductDAO extends ServiceImpl<EmsSyncIotProductMapper, EmsSyncIotProduct> {

    public Page<EmsSyncIotProduct> selectByPage(String search, Integer pageNo, Integer pageSize){
        Page<EmsSyncIotProduct> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<EmsSyncIotProduct> wrapper = Wrappers.lambdaQuery();
        wrapper.and(!StringUtils.isBlank(search),d->d.like(EmsSyncIotProduct::getProductKey,search).or().like(EmsSyncIotProduct::getSimpleName,search));
        wrapper.orderByDesc(EmsSyncIotProduct::getSyncTime);
        return this.baseMapper.selectPage(page,wrapper);
    }

    /**
     * 通过productKey集合删除数据
     * @param productKeyList: 产品key集合
     * @return
     */
    public void deleteByProductKeys(List<String> productKeyList){
        if(CollectionUtils.isEmpty(productKeyList)){
            return;
        }
        LambdaQueryWrapper<EmsSyncIotProduct> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsSyncIotProduct::getProductKey,productKeyList);
        this.baseMapper.delete(lambdaQueryWrapper);
    }

    public List<EmsSyncIotProduct> selectByProductKeys(List<String> productKeys){
        LambdaQueryWrapper<EmsSyncIotProduct> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmsSyncIotProduct::getProductKey,productKeys);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

}
